package com.gosophia.conferenceManagement.confPreparation.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.gosophia.commons.exception.BusinessException;
import com.gosophia.conferenceManagement.ConferenceTurningAction;
import com.gosophia.conferenceManagement.common.dao.DocumentDao;
import com.gosophia.conferenceManagement.common.entity.Document;
import com.gosophia.conferenceManagement.confPreparation.dao.ConfPreparationJobDataDao;
import com.gosophia.conferenceManagement.confPreparation.dao.NotificationFeedbackListDao;
import com.gosophia.conferenceManagement.confPreparation.dao.ParticipatingNameListDao;
import com.gosophia.conferenceManagement.confPreparation.entity.ConfPreparationJobData;
import com.gosophia.conferenceManagement.confPreparation.entity.NotificationFeedbackList;
import com.gosophia.conferenceManagement.confPreparation.entity.ParticipatingNameList;
import com.gosophia.conferenceManagement.confPreparation.service.ConfPreparationService;
import com.gosophia.taskManagement.entity.Job;
import com.gosophia.taskManagement.service.TaskManagementService;
import com.gosophia.userManagement.entity.UserInfo;
import com.gosophia.userManagement.service.UserManagementService;
/**
 * 会务管理接口实现
 * @author wangxinbing
 * @since  2011-3-24
 *  Copyright (c) 2010 Gosophia corporation All Rights Reserved
 */
@Transactional
@Service("confPreparationService")
public class ConfPreparationServiceImpl implements ConfPreparationService {
    @Autowired
    private ConfPreparationJobDataDao confPreJobDataDao;
    @Autowired
    private NotificationFeedbackListDao feedBackListDao;
    @Autowired
    private TaskManagementService taskManagementService;
    @Autowired
    private UserManagementService userManagementService;
    @Autowired
	private ParticipatingNameListDao particiPateDao;
    @Autowired
    private DocumentDao documentDao;
    
	@Override
	public void addNotificationFeedbackList(final Long jobId,
			final List<UserInfo> userInfoList) throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		final List<NotificationFeedbackList> noticeFeedBackLists = 
			                 new ArrayList<NotificationFeedbackList>();
		for(final UserInfo userInfo : userInfoList){
			final NotificationFeedbackList noticeF = new NotificationFeedbackList();
			noticeF.setUserInfo(userInfo);
			//是否参加 默认值为null
			noticeF.setIfAttend("");
			noticeFeedBackLists.add(noticeF);
		}
		final Job job = taskManagementService.findJobDetailByJobId(jobId);
		ConfPreparationJobData confPJobData = (ConfPreparationJobData) taskManagementService
				.findJobDataDetail(jobId, ConfPreparationJobData.class);
		if(confPJobData == null){
			confPJobData = new ConfPreparationJobData();
			confPJobData.setJob(job);
			confPJobData.setNotificationFeedbackLists(noticeFeedBackLists);
			confPreJobDataDao.save(confPJobData);
		}else{
			confPJobData.setNotificationFeedbackLists(noticeFeedBackLists);
			confPreJobDataDao.update(confPJobData);
		}
		//清楚缓存
		confPreJobDataDao.clearSession();
	}

	@Override
	public List<NotificationFeedbackList> findNotificationFeedbackListsByJobId(
			final Long jobId) {
		Assert.notNull(jobId,"jobId不能为null");
		final List<NotificationFeedbackList> feedbackLists = feedBackListDao.
		     getListByNamedQuery("confPreparationService.findNotificationFeedbackListsByJobId", jobId);
		return feedbackLists;
	}

	@Override
	public List<ParticipatingNameList> findParticipatingNameListsByJobId(
			final Long jobId) {
		Assert.notNull(jobId,"jobId不能为null");
		final List<ParticipatingNameList> particNameList = particiPateDao.
		    getListByNamedQuery("confPreparationService.findParticipatingNameListsByJobId", jobId);
		return particNameList;
	}

	@Override
	public void updateNotificationFeedbackList(final Long jobId, final Long userInfoId,
			final String ifAttend) throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		Assert.notNull(userInfoId,"userInfoId不能为null");
		Assert.notNull(ifAttend,"ifAttend不能为null");
		final NotificationFeedbackList notifyFeedBack = feedBackListDao.
		        getUniqueByNamedQuery("confPreparationService.findFeedBackDetailByJobIdAndUserinfoId", jobId,userInfoId);
		if(notifyFeedBack.getIfAttend() ==null){
			notifyFeedBack.setIfAttend(ifAttend);
			feedBackListDao.update(notifyFeedBack);
		}
	}

	@Override
	public void updateParticipatingNameList(final Long jobId,
			final List<ParticipatingNameList> participatingNameLists)
			throws BusinessException {
		Assert.notNull(jobId, "jobId不能为null");
		final Job job = taskManagementService.findJobDetailByJobId(jobId);
		ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.
		                       findJobDataDetail(jobId, ConfPreparationJobData.class);
		//清除缓存
		confPreJobDataDao.clearSession();
		if(confPJobData == null){
			confPJobData = new ConfPreparationJobData();
			confPJobData.setJob(job);
			confPJobData.setParticipatingNameLists(participatingNameLists);
			confPreJobDataDao.save(confPJobData);
		}else{
			final Long jobDataId = confPJobData.getJobDataId();
			particiPateDao.executeNamedQuery("confPreparationService.deleteParticipatingNameListsByJobDataId", jobDataId);
			confPJobData.setParticipatingNameLists(participatingNameLists);
			confPreJobDataDao.update(confPJobData);
		}


	}

	@Override
	public Document findAgendaByJobId(final Long jobId) throws BusinessException{
		Assert.notNull(jobId, "jobId不能为null");
		Document agenda = new Document();
	    final ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.findJobDataDetail(jobId, ConfPreparationJobData.class);
		if(confPJobData !=null){
			agenda = confPJobData.getAgenda();
		}
	    return agenda;
	}

	@Override
	public List<Document> findMaterialsByJobId(final Long jobId) throws BusinessException{
		Assert.notNull(jobId, "jobId不能为null");
		List<Document> materials = new ArrayList<Document>();
	    final ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.findJobDataDetail(jobId, ConfPreparationJobData.class);
		if(confPJobData!=null){
			materials = confPJobData.getMaterials();
		}
		return materials;
	    
	}

	@Override
	public Document findSignTableByJodId(final Long jobId) throws BusinessException{
		Assert.notNull(jobId, "jobId不能为null");
		Document signTable = new Document();
	    final ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.findJobDataDetail(jobId, ConfPreparationJobData.class);
	    if(confPJobData!=null){
	    	signTable = confPJobData.getCheckInTable();
		}
	    return signTable;
	}

	@Override
	public void updateConfMaterials(final Long jobId, final List<Document> materials)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		Job job = taskManagementService.findJobDetailByJobId(jobId);
		ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.findJobDataDetail(jobId, ConfPreparationJobData.class);
		//清除缓存
		confPreJobDataDao.clearSession();
		if(confPJobData == null){
			confPJobData = new ConfPreparationJobData();
			confPJobData.setJob(job);
			confPJobData.setMaterials(materials);
			confPreJobDataDao.save(confPJobData);
		}else{
			final List<Document> docs = documentDao.getListByNamedQuery("confPreparationService.findDocByJobId", jobId);
			for(final Document doc : docs){
				documentDao.delete(doc);
			}
			documentDao.clearSession();
			confPJobData.setMaterials(materials);
			confPreJobDataDao.update(confPJobData);
		}
		
	}

	@Override
	public Document updateAgenda(final Long jobId, final Document doc)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		final Job job = taskManagementService.findJobDetailByJobId(jobId);
		ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.findJobDataDetail(jobId, ConfPreparationJobData.class);
		//清除缓存
		confPreJobDataDao.clearSession();
		if(confPJobData == null){
			confPJobData = new ConfPreparationJobData();
			confPJobData.setJob(job);
			confPJobData.setAgenda(doc);
			confPreJobDataDao.save(confPJobData);
		}else{
			Document agenda = confPJobData.getAgenda();
			if(agenda == null){
				agenda = new Document();
			}
			agenda.setDocumentCategory(doc.getDocumentCategory());
			agenda.setDocumentName(doc.getDocumentName());
			agenda.setDocumentNote(doc.getDocumentNote());
			agenda.setDocumentPath(doc.getDocumentPath());
			agenda.setDocumentVersion(doc.getDocumentVersion());
			if(agenda.getDocumentId() != null){
				documentDao.update(agenda);
			}else{
				documentDao.save(agenda);
				documentDao.clearSession();
				confPJobData.setAgenda(agenda);
				confPreJobDataDao.update(confPJobData);
			}
		}
		return confPJobData.getAgenda();
	}

	@Override
	public Document updateSignTable(final Long jobId, final Document doc)
			throws BusinessException {
		Assert.notNull(jobId,"jobId不能为null");
		Job job = taskManagementService.findJobDetailByJobId(jobId);
		ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.findJobDataDetail(jobId, ConfPreparationJobData.class);
		//清除缓存
		confPreJobDataDao.clearSession();
		if(confPJobData == null){
			confPJobData = new ConfPreparationJobData();
			confPJobData.setJob(job);
			confPJobData.setCheckInTable(doc);
			confPreJobDataDao.save(confPJobData);
		}else{
			Document signTable = confPJobData.getCheckInTable();
			if(signTable ==null){
				signTable = new Document();
			}
			signTable.setDocumentCategory(doc.getDocumentCategory());
			signTable.setDocumentName(doc.getDocumentName());
			signTable.setDocumentNote(doc.getDocumentNote());
			signTable.setDocumentPath(doc.getDocumentPath());
			signTable.setDocumentVersion(doc.getDocumentVersion());
			if(signTable.getDocumentId() != null){
				documentDao.update(signTable);
			}else{
				documentDao.save(signTable);
				documentDao.clearSession();
				confPJobData.setCheckInTable(signTable);
				confPreJobDataDao.update(confPJobData);
			}
			
		}
		return confPJobData.getCheckInTable();
	}

	@Override
	public void accomplishConferencePrepare(final Long jobId,final Long operatorId,String confPrepareType)
			throws BusinessException {
		final Job confPrepare = taskManagementService.findJobDetailByJobId(jobId);
		
		if(confPrepare ==null){
			throw new BusinessException("该Job不存在", "JC2_CM_B_30");
		}
		 ConfPreparationJobData  jobData;
		if(confPrepare.getActualEndTime() ==null && ("会务准备").equals(confPrepare.getJobType())){
			jobData = (ConfPreparationJobData)taskManagementService.
				                          findJobDataDetail(jobId, ConfPreparationJobData.class);
			Job job = jobData.getJob();
			documentDao.clearSession();
			
			if(ConferenceTurningAction.JUMP_ACTION.getValue().equals(confPrepare)){
				jobData = new ConfPreparationJobData();
				jobData.setJob(job);
				//TODO跳过要保存的属性值
			}
			taskManagementService.processJob(jobData, "已完成", "会议总结起草", jobId, operatorId, "会务完成确认");
				
		}else{
		   throw new BusinessException("该Job已完成", "JC2_CM_B_31");
		}
	}

	@Override
	public boolean ifFeedBacked(final Long jobId, final Long userInfoId) {
		Assert.notNull(jobId,"jobId不能为null");
		Assert.notNull(userInfoId,"userInfoId不能为null");
		final NotificationFeedbackList notifyFeedBack = feedBackListDao.getUniqueByNamedQuery(
				"confPreparationService.findFeedBackDetailByJobIdAndUserinfoId", jobId,userInfoId);
		if(notifyFeedBack!=null && notifyFeedBack.getIfAttend()!=null){
			return true;
		}else{
			return false;
		}
	}

	@Override
	public void deleteConfPMetailBydocId(Long documentId,Long jobId)
			throws BusinessException {
		Assert.notNull(documentId,"documentId不能为null");
		Assert.notNull(jobId,"jobId不能为null");
		Document docDelete = documentDao.get(documentId);
		if(docDelete!=null){
			documentDao.delete(docDelete);
		}else{
			throw new BusinessException("该附件已被删除", "JC2_CM_B_32");
		}
		
//		ConfPreparationJobData confPJobData = (ConfPreparationJobData)taskManagementService.findJobDataDetail(jobId, ConfPreparationJobData.class);
		//清除缓存
//		confPreJobDataDao.clearSession();
//
//	    List<Document> docs = documentDao.getListByNamedQuery("confPreparationService.findDocByJobId", jobId);
//		for(Document doc : docs){
//			documentDao.delete(doc);
//		}
//		documentDao.clearSession();
//		for(Document doc1 : docs){
//			if(doc1.getDocumentId() == documentId){
//				docs.remove(doc1);
//			}
//		}
//		List<Document> materials = new ArrayList<Document>();
//		materials.addAll(docs);
//		confPJobData.setMaterials(materials);
//		confPreJobDataDao.update(confPJobData);
	}

}
